package com.sghd.candy.module.box.service;

import com.sghd.candy.module.box.manager.BoxKeysConfig;
import com.sghd.candy.module.box.manager.BoxKeysConfigManager;
import com.sghd.pojo.StaticConfig;
import com.sghd.pojo.candy.box.enums.ScopeType;
import com.sghd.pojo.candy.fight.enums.BattleResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Allen
 * @date 2018/12/18
 */
@Service
public class BoxService {
    @Autowired
    BoxKeysConfigManager boxKeysConfigManager;

    public BoxKeysConfig getDropBoxKey(ScopeType scopeType, BattleResult battleResult) {
        List<BoxKeysConfig> list = boxKeysConfigManager.findAll(scopeType);
        double random = Math.random();
        float currentFloat = 0F;
        float off = 1.0F;

        if (battleResult == BattleResult.LOSE) {
            off = off * StaticConfig.LOSE_OFF;
        }

        for (BoxKeysConfig boxKeysConfig : list) {
            if (random > currentFloat && random <= currentFloat + boxKeysConfig.getDropRate() * off) {
                return boxKeysConfig;
            } else {
                currentFloat = currentFloat + boxKeysConfig.getDropRate() * off;
            }
        }

        return null;
    }
}
